package discount

// PercentDiscount 百分比折扣
type PercentDiscount struct {
	DiscountPercent float64 // 折扣百分比(0.8表示8折)
}

// NewPercentDiscount 新建百分比折扣
func NewPercentDiscount(discountPercent float64) *PercentDiscount {
	return &PercentDiscount{
		DiscountPercent: discountPercent,
	}
}

// CalculateTotalAmount 计算折扣后的金额
func (p *PercentDiscount) CalculateTotalAmount(ctx *DiscountContext) float64 {
	total := 0.0
	for _, item := range ctx.Order.Items {
		total += item.Product.Price * float64(item.Quantity)
	}
	return total * p.DiscountPercent
}

// Name 策略名称
func (p *PercentDiscount) Name() string {
	return "百分比折扣"
}